home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1991 / 01 / textbuf.h < prev    next >
Text File  |  1990-10-31  |  6KB  |  121 lines

  1. /* textbuf.h -- Listing 1. */
  2.  
  3. #ifndef __TEXTBUF_H     /* Make sure that textbuf.h isn't included */
  4. #define __TEXTBUF_H     /* twice, the endif is on the last line.   */
  5.  
  6. #ifdef NEAR_HEAP
  7. #   define BMALLOC   malloc     /* Allocate memory for buffer.  */
  8. #   define BFREE     free       /* Free memory used for buffer. */
  9. #   define MAXBSIZE  4096       /* Maximum row * col size.      */
  10.     typedef char     *bufptr;   /* pointer to the buffer        */
  11.     typedef int      bufsize;   /* holds max buffer size        */
  12.  
  13. #   define bufset( dst,c,  size) memset (dst,  c,  size)
  14. #   define bufmove(dst,src,size) memmove(dst, src, size)
  15. #else
  16. #   include <alloc.h>           /* needed for farmalloc prototype */
  17. #   define BMALLOC    farmalloc
  18. #   define BFREE      farfree
  19. #   define MAXBSIZE   (0xffffU - 1)  /* 64K - 1 */
  20.     typedef char far* bufptr;
  21.     typedef unsigned  bufsize;
  22.  
  23.     extern void _bufset  ( bufptr p, const int c, bufsize size  );
  24.     extern void _bufmove ( bufptr dst, bufptr src, bufsize size );
  25. #   define bufset(p,c,s)        _bufset(p,c,s)  /* in textbuf.c */
  26. #   define bufmove(d,s,size)    _bufmove(d,s,size)
  27. #endif
  28.  
  29. #define PUBLIC  /* for documentation: globally accessible object. */
  30.  
  31. typedef struct
  32. {
  33.     unsigned magic; /* Magic number for verification    */
  34.     int nrows;      /* Number of rows                   */
  35.     int ncols;      /* Number of columns                */
  36.     int row;        /* Current cursor position, row     */
  37.     int col;        /* Current cursor position, column  */
  38.     bufptr p;       /* Character at that position       */
  39.     bufptr buf;     /* Actual buffer.                   */
  40. }
  41. textbuf;
  42.  
  43. /* B_LAST_LINE is passed a textbuf pointer and returns a pointer to
  44.  * the beginning of the last line of the buffer. The first line is
  45.  * pointed to by "buf."
  46.  *
  47.  * textbuf.magic is set to B_MAGIC when a textbuf is allocated. Use
  48.  * it to verify that a textbuf pointer is reasonable.
  49.  */
  50. #define B_LAST_LINE(b)  ((b)->buf + (((b)->nrows - 1) * (b)->ncols))
  51. #define B_MAGIC 0xabcdU
  52. /*----------------------------------------------------------------------*/
  53. PUBLIC textbuf *b_new( int nrows, int ncols );
  54. PUBLIC void     b_delete( textbuf *b );
  55.  
  56. PUBLIC textbuf *b_construct( textbuf *b, int nrows, int ncols );
  57. PUBLIC int      b_destruct( textbuf *b );
  58. /*----------------------------------------------------------------------*/
  59. #define b_current( b )  (  (b)->p       )
  60. #define b_advance( b )  (  (b)->col++,  \
  61.                            (b)->p++     \
  62.                         )
  63. #define b_row(b)        (  (b)->row     )
  64. #define b_col(b)        (  (b)->col     )
  65. #define b_nrows(b)      (  (b)->nrows   )
  66. #define b_ncols(b)      (  (b)->ncols   )
  67. #define b_setrc(b,r,c)  (  ((b)->row = (r)),                            \
  68.                            ((b)->col = (c)),                            \
  69.                            ((b)->p   = (b)->buf + ((r)*(b)->ncols)+(c)) \
  70.                         )
  71. #define b_ateol(b)      ( (b)->col == ((b)->ncols - 1) )
  72. #define b_ateob(b)      ( (b)->row == ((b)->nrows - 1) )
  73. #define b_valid(b,r,c)  ( (b)                                   \
  74.                           && ((b)->magic == B_MAGIC)            \
  75.                           && (0 <= (r) && (r) < (b)->nrows)     \
  76.                           && (0 <= (c) && (c) < (b)->ncols)     \
  77.                         )
  78. #define b_clearbuf(b,c) ( b_setrc(b,0,0),                               \
  79.                           bufset((b)->buf, (c), (b)->nrows * (b)->ncols)\
  80.                         )
  81. #define b_cr(b)         ( ((b)->p -= (b)->col ),        \
  82.                           ((b)->col = 0       )         \
  83.                         )
  84. #define b_lf(b)         (  ( ++(b)->row           ),    \
  85.                            ( (b)->p += (b)->ncols )     \
  86.                         )
  87. #define b_clearline(b,c)  (  b_cr(b),                         \
  88.                              bufset( (b)->p, (c), (b)->ncols )  \
  89.                           )
  90. #define b_available(b)    (  (b)->ncols - (b)->col )
  91. #define b_insert_c(b,n,c) (  bufmove( (b)->p + n, (b)->p,               \
  92.                                                 b_available(b) - (n) ), \
  93.                              bufset ( (b)->p    , (c)   , (n) )         \
  94.                           )
  95. #define b_delete_c(b,n,c) (  bufmove( (b)->p, (b)->p+(n), (n) ),        \
  96.                              bufset ( ( (b)->p - (b)->col) +            \
  97.                                       ( (b)->ncols - (n) ), (c), (n))   \
  98.                           )
  99. #define b_closeup(b,c)    ( b_cr(b),                                    \
  100.                             bufmove( (b)->buf + (b)->ncols, (b)->buf,   \
  101.                                                 (b)->row * (b)->ncols ),\
  102.                             bufset( (b)->buf, (c), (b)->ncols )         \
  103.                           )
  104. #define b_closedown(b,c)  ( b_cr(b),                                    \
  105.                             bufmove( (b)->p, (b)->p + (b)->ncols,       \
  106.                                   ((b)->nrows-(b)->row-1) * (b)->ncols),\
  107.                             bufset( B_LAST_LINE(b), (c), (b)->ncols )   \
  108.                           )
  109. #define b_openup(b, c)    (  b_cr(b),                                   \
  110.                              bufmove( (b)->buf, (b)->buf + (b)->ncols,  \
  111.                                                 (b)->row * (b)->ncols ),\
  112.                              bufset( (b)->p, (c), (b)->ncols )          \
  113.                           )
  114. #define b_opendown(b,c)   ( b_cr(b),                                    \
  115.                             bufmove( (b)->p + (b)->ncols, (b)->p,       \
  116.                                     ((b)->nrows-(b)->row-1)*(b)->ncols),\
  117.                             bufset( (b)->p, (c), (b)->ncols )           \
  118.                           )
  119. #endif /* #ifndef __TEXTBUF_H */
  120.  
  121.